//
//  ViewController.swift
//  Space Experiment Module
//
//  Created by 曹亚索 on 2020/12/17.
//

import UIKit
import LeanCloud
import SwiftyJSON
class MainViewController: UIViewController ,selectedDelegate{
    func selectedGet(renwu: UIImage) {
        currentCharacterView.image = renwu
    }
    
    var label : UILabel!
    @IBOutlet weak var currentCharacterView: UIImageView!
    @IBOutlet weak var AQswitch: UISwitch!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //MARK:ALL CLEAR
//        allClear()
        //网络请求
        AQ().internetRequst()
        //排行榜初始化
        loopGetRank()
        //初始化
        Initial()
        //游戏界面生命值设置初始化
        setupHealthArray(num: 10)
        PlantsCharacter().setupHealViewArray(num: 10)
    }
    
    @IBAction func showRank(_ sender: Any) {
        self.view.alpha = 0.5
        Alert().rankAlert() { (closerank) in
            self.view.alpha = 1
        }
    }

    @IBAction func AQswitch(_ sender: Any) {
        if AQSwitch == false{
            AQSwitch = true
            AQ().internetRequst()
        }else{
            AQSwitch = false
        }
    }
    
    func loopGetRank(){
        
        let query = LCQuery(className: "Rank")
        let _ = query.get("60a3b79efaa10e45649916fb") { (result) in
            switch result {
            case .success(object: let todo):
                let result = todo.get("ScoreArray")?.arrayValue
                //取其中最高5个
                for i in 0...4{
                    let a = result![i] as! Double
                    let score = Int(a)
                    scoreArray.append(score)
                }
            case .failure(error: let error):
                print(error)
            }
        }
 
//        for object in objectIdArray{
//            let query = LCQuery(className: "Rank")
//            let _ = query.get(object) { (result) in
//                switch result {
//                case .success(object: let Rank):
//                    let scroll = Int((Rank.get("Score")?.intValue)!)
//                    let objectNO = Int((Rank.get("objectNO")?.intValue)!)
//                    if objectNO == 1{
//                        no1Score = scroll
//                    }else if objectNO == 2{
//                        no2Score = scroll
//                    }else if objectNO == 3{
//                        no3Score = scroll
//                    }else if objectNO == 4{
//                        no4Score = scroll
//                    }else if objectNO == 5{
//                        no5Score = scroll
//                    }
//                    //得到五个可选型的Number
//                    scoreArray = [no1Score,no2Score,no3Score,no4Score,no5Score]
//                case .failure(error: let error):
//                    print(error)
//                }
//            }
//        }
    }
    
    func Initial(){
        //data初始化
        getdata()
//        assets = 2000
        //character health
        Tomato.health = Tomato.Level
        Chili.health = Chili.Level
        Eggplant.health = Eggplant.Level
        //character image
        Tomato.image = UIImage(named: characterIdArray[0])
        Chili.image = UIImage(named: characterIdArray[1])
        Eggplant.image = UIImage(named: characterIdArray[2])
        //insect image
        Tomato.insectImage = UIImage(named: "tomatoinsect")
        Chili.insectImage = UIImage(named: "chiliinsect")
        Eggplant.insectImage = UIImage(named: "eggplantinsect")
        //界面初始化
        currentCharacterView.image = Chili.image
        AQswitch.setOn(false, animated: false)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "tocharacter"{
            let vc = segue.destination as! PlantsCharacter
            vc.SC = self
        }
    }
    
    func allClear(){
        assets = 0
        Tomato.Level = 1
        Chili.Level = 1
        Eggplant.Level = 1
        setdata()
    }
    
    
}


